const express = require('express')
// const app = express()
// app.use(express.urlencoded({ extended: false }))

const router = express.Router()

const adminHandler = require('../../router_handler/admin/admin')

const verifyToken = (req, res, next) => {
    const bearerHeader = req.headers.authorization
    if (typeof bearerHeader !== 'undefined') {
        req.token = bearerHeader.split(' ')[1]
        next()
    } else {
        res.send({
            code: 401,
            msg: '请先登录！',
            data: {}
        })
    }
    next()
}

//登录
router.post('/admin/login', adminHandler.login)
//获取管理员信息
router.get('/admin/info', adminHandler.getInfo)

//增加管理员
router.post('/admin/admin', adminHandler.addAdmin)

//更新管理员
router.put('/admin/admin', adminHandler.updateAdmin)

//更新管理员
router.delete('/admin/admin', adminHandler.delAdmin)

//获取管理员列表
router.get('/admin/admin', adminHandler.getAdminList)

//获取数据量
router.get('/admin/countData', adminHandler.getCountData)

//退出登录
router.post('/admin/logout', verifyToken, adminHandler.logout)

module.exports = router